package com.ui;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bean.SearchHotKeyBean;
import com.ddd.wandemo.R;

import java.util.ArrayList;
import java.util.List;

public class DaySelectAdapter extends RecyclerView.Adapter<DaySelectAdapter.TestAdapterViewHolder> {
    private List<SearchHotKeyBean> dataList = new ArrayList<>();
    private Context mContext;
    private int oldIndex;
    private ItemPositionListener mListener;

    public DaySelectAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public TestAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_day_select, parent, false);
        return new TestAdapterViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final TestAdapterViewHolder holder, final int position) {
        if (dataList.get(position).getVisible() == 1) {
            holder.tv.setSelected(true);
            oldIndex = position;
            mListener.onItemPositionListener(oldIndex);
        } else {
            holder.tv.setSelected(false);
        }
        holder.tv.setText(dataList.get(position).getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.get(oldIndex).setVisible(0);
                dataList.get(position).setVisible(1);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    public class TestAdapterViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;

        public TestAdapterViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }

    public void setData(List<SearchHotKeyBean> data) {
        if (dataList != null) {
            dataList.clear();
            dataList.addAll(data);
        }
        notifyDataSetChanged();
    }

    public void changePage(int index){
        dataList.get(oldIndex).setVisible(0);
        dataList.get(index).setVisible(1);
        oldIndex = index;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                notifyDataSetChanged();
            }
        },100);
    }

    public interface ItemPositionListener {
        void onItemPositionListener(int position);
    }

    public void setOnItemPositionListener(ItemPositionListener listener){
        mListener = listener;
    }
}
